package com.github.thinwonton.springboot.showcase.poi.easypoi;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.github.thinwonton.springboot.showcase.poi.easypoi.entity.CompositeExcelVO;
import com.github.thinwonton.springboot.showcase.poi.easypoi.entity.Student;
import com.github.thinwonton.springboot.showcase.poi.easypoi.entity.User;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * EasyPOI 测试一对多
 *
 * @author hugo
 * @date 2018/9/18
 */
public class EasyPOICollectionMain {
    private static final String excelFilePath = "file/demo.xlsx";

    public static void main(String[] args) throws IOException {
//        File file = classpathFile(excelFilePath);

        File file = new File("D:/课程表.xls");

        ImportParams params = new ImportParams();
        params.setStartSheetIndex(0); //从模板中第三个sheet读取
        params.setStartRows(1); //数据距离标题的行数
        params.setTitleRows(0); //标题行号
        //验证数据
        params.setNeedVerfiy(true);
        //验证数据数据，在数据库中验证
        params.setVerifyHandler(new IExcelVerifyHandler<CompositeExcelVO>() {
            @Override
            public ExcelVerifyHandlerResult verifyHandler(CompositeExcelVO compositeExcelVO) {
                Student student = compositeExcelVO.getStudent();
                String name = student.getName();
                if ("奥巴马".equals(name)) {
                    return new ExcelVerifyHandlerResult(false,"数据库已经存在该数据");
                }
                return new ExcelVerifyHandlerResult(true);
            }
        });

        ExcelImportResult<CompositeExcelVO> result = ExcelImportUtil.importExcelMore(file, CompositeExcelVO.class, params);

        List<CompositeExcelVO> successList = result.getList();
        List<CompositeExcelVO> failList = result.getFailList();

        System.out.println("是否存在验证未通过的数据:" + result.isVerfiyFail());
        System.out.println("验证通过的数量:" + successList.size());
        System.out.println("验证未通过的数量:" + failList.size());

        System.out.println("aaaa");

    }

    private static File classpathFile(String path) {
        URL systemResource = ClassLoader.getSystemResource(path);
        return new File(systemResource.getFile());
    }

}
